import { defineStore } from 'pinia'
import { ref, Ref, computed } from 'vue'
import type { GoodsDetailType } from '../types/Goos'

export const useCartStore = defineStore('cart', () => {
  // 下面就写 组合式 api --- setup store --- 符合vue3的 组合式api的写法
  // state --- ref
  // const cartList = ref<GoodsDetailType[]>([])
  const cartList: Ref<GoodsDetailType[]> = ref<GoodsDetailType[]>([])

  // ts里面 通过ref 定义 数组和对象，一定要指明类型，不能不写，ts自动类型推导的 never
  const aaa = ref<any[]>([])

  // getters  --- computed
  const filterCartList = computed(() => {
    return cartList.value.filter(item => (item as any).aaaa)
    // return cartList.value.filter(item => item.checked)
  })

  // action -- 不分同步异步
  const updateCart = (payload: any): void => {
    cartList.value.push(payload)
  } 

  return {cartList, filterCartList, updateCart}
})